package tech.eyao.hs60x.serviceImpl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import cn.jiguang.common.utils.StringUtils;
import tech.eyao.hs60x.common.ConstantsKey;
import tech.eyao.hs60x.converts.ZoneConvert;
import tech.eyao.hs60x.dao.ZoneDao;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.enums.ExceptionCodeEnum;
import tech.eyao.hs60x.enums.Status;
import tech.eyao.hs60x.model.domain.SafetyZoneDO;
import tech.eyao.hs60x.service.ZoneService;

@Service
public class ZoneServiceImpl implements ZoneService {
	@Autowired
	private ZoneDao zoneDao;
	@Autowired
	private StringRedisTemplate template;
	private static final Logger looger = LoggerFactory.getLogger(ZoneServiceImpl.class);

	@Override
	public BaseResult set(long id,long accountId, long watchId, String zoneName, double longitude, double latitude, int r, String address) {
		if(StringUtils.isEmpty(zoneName) || longitude <=0 || latitude <= 0 || r <= 0 ||StringUtils.isEmpty(address)) {
			return BaseResult.isBussinessFail(ExceptionCodeEnum.INVALID_PARAMETER.getMessage());
		}
		try {
			boolean status;
			SafetyZoneDO safetyZoneDO = zoneDao.selectByPrimaryKey(id);
			if(null != safetyZoneDO) {
				safetyZoneDO.setAddress(address);
				safetyZoneDO.setLatitude(latitude);
				safetyZoneDO.setLongitude(longitude);
				safetyZoneDO.setR(r);
				safetyZoneDO.setStatus(Status.ONLINE.getValue());
				safetyZoneDO.setType(1);
				safetyZoneDO.setWatchId(watchId);
				safetyZoneDO.setZoneName(zoneName);
				status = zoneDao.updateByPrimaryKey(safetyZoneDO);
			} else {
				safetyZoneDO = new SafetyZoneDO();
				safetyZoneDO.setAddress(address);
				safetyZoneDO.setLatitude(latitude);
				safetyZoneDO.setLongitude(longitude);
				safetyZoneDO.setR(r);
				safetyZoneDO.setStatus(Status.ONLINE.getValue());
				safetyZoneDO.setType(1);
				safetyZoneDO.setWatchId(watchId);
				safetyZoneDO.setZoneName(zoneName);
				status = zoneDao.insert(safetyZoneDO);
			}
			if(!status) {
				return BaseResult.isDeFaultFailure();
			}
		} catch (Exception e) {
			looger.error("[ZoneService set error.]", e);
		}
		return BaseResult.isSuccess();
	}

	@Override
	public BaseResult delete(long userId, long watchId, long id) {
		try {
			
			if(userId <= 0 || watchId <=0) {
				return BaseResult.isBussinessFail("参数非法");
			}
			SafetyZoneDO safetyZoneDO = zoneDao.selectByPrimaryKey(id);
			if(null == safetyZoneDO) {
				return BaseResult.isDeFaultNoData();
			}
			
			String adminAccountId = template.opsForValue().get(ConstantsKey.getWatchAdminKey(watchId));
			if(userId != Long.parseLong(adminAccountId)) {
				return BaseResult.isBussinessFail("非手表管理员");
			}
			
			boolean status = zoneDao.deleteByPrimaryKey(id);
			if(!status) {
				return BaseResult.isDeFaultFailure();
			}
		} catch (Exception e) {
			looger.error("[ZoneService delete error.]", e);
		}
		
		return BaseResult.isSuccess();
	}

	@Override
	public BaseResult list(long userId, long watchId) {
		try {
			List<SafetyZoneDO> list = zoneDao.list(watchId);
			return BaseResult.isSuccess(ZoneConvert.convertList(list));
		} catch (Exception e) {
			looger.error("[ZoneService list error.]", e);
		}
		return null;
	}
}
